﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Alerts.Interfaces.ProfileSettings;
using Sitecore.Data.Items;

namespace Workflow.Alerts.Sublayouts.ProfileSettings
{
    public partial class ContactPersonList : BaseControl, IPersonListView
    {
        #region Fields
        int id;
        #endregion

        #region Properties
        public string Path
        {
            set
            {
                DetailsPath.Value = value;
            }
        }
        #endregion

        #region IContactPersonListView Members

        public System.Data.DataTable Contacts
        {
            set
            {
                RadGrid1.DataSource = value;
                RadGrid1.DataBind();
            }
        }

        public event OnContactPersonListViewLoad OnPageLoad;

        public int? NetworkOperatorId
        {
            get
            {   
                if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
                {
                    return (Session[WorkflowConstants.CurrentUser] as User).TypeId;
                }
                if (Request.QueryString["opId"] != null && Int32.TryParse(Request.QueryString["opId"], out id))
                {
                    return id;
                }
                return null;
            }
        }

        #endregion

        #region Events
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Item detailPageItem = Sitecore.Context.Item.Axes.GetChild("New Contact");
            var path = Sitecore.Links.LinkManager.GetItemUrl(detailPageItem);
            if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
            {
                Response.Redirect(path);
            }
            else
            {
                Response.Redirect(GetNetworkId(path));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Item detailPageItem = Sitecore.Context.Item.Axes.GetChild("Contact Details");
            var path = Sitecore.Links.LinkManager.GetItemUrl(detailPageItem);
            if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
            {
                Path = path;
            }
            else
            {
                Path = GetNetworkId(path);
            }

            PresentationManager.RegisterView(typeof(PersonListPresenter), this, new WebSessionProvider());
            if (OnPageLoad != null)
            {
                OnPageLoad();
            }
        }

        #endregion

        #region Private Methods
        private string GetNetworkId(string path)
        {
            return string.Format("{0}?opId={1}", path, NetworkOperatorId.Value);
        }
        private string GetNetworkId(string path, int id)
        {
            return string.Format("{0}?opId={1}", path, id);
        }
        #endregion
    }
}